#include <stdio.h>
//表示顺序表申请的空间大小
#define maxSize  5

int enQueue(int * a, int front, int rear, int data) {
    //添加判断语句，如果rear+1和front重合，则表示数组已满
    if(front == (rear+1)%maxSize) {
        printf("队列满\n");
        return rear;
    }
    //如果rear超过max，则直接将其从下表取余存储
    a[rear%maxSize] = data;
    rear++;
    return rear;
}

int deQueue(int * a, int front, int rear) {
    //如果front==rear，表示队列为空
    if(front == rear%maxSize) {
        printf("队列空\n");
        return front;
    }
    printf("%d ",a[front]);
    //front不再直接 +1，而是+1后同maxSize进行比较，如果=maxSize，则直接将其从下表取余
    front = (front+1)%maxSize;
    return front;
}

int main(int argc, char * argv[]) {
    int a[maxSize];
    int front, rear;
    //设置队头指针和队尾指针，当队列中没有元素时，队头和队尾指向同一块地址
    front = rear = 0;
    //入队
    for(int i = 1; i < 5; i++) {
        rear = enQueue(a, front, rear, i);
    }
    //出队
    front = deQueue(a, front, rear);
    //再入队
    rear = enQueue(a, front, rear, 5);
    //再出队
    front = deQueue(a, front, rear);
    //再入队
    rear = enQueue(a, front, rear, 6);
    //再出队
    for(int i = 0; i < 4; i++) {
        front = deQueue(a, front, rear);
    }
    return 0;
    
    
}